#include <stdio.h>
#include "ofc/DUDPServer.h"
#include "ofc/DSocketAddress.h"

@interface MyServer : DUDPServer
{
}

- (BOOL) handleRequest :(DData *) request :(DData *) response;

@end

@implementation MyServer

- (BOOL) handleRequest :(DData *) request :(DData *) response
{
  DText *str;
 
  str = [request toText];              // Print the response
  printf("Server receives the request:%s\n", [str cstring]);
  [str free];

  printf(".. and sends the response: Nice weather.\n");

  [response set :"Nice weather" :12];  // Send the response

  return YES;                          // Example stop the server
}

@end

int main(int argc, char *argv[])
{
  MyServer           *srvr = [MyServer       new];
  DInetSocketAddress *addr = [DInetSocketAddress new];

  [addr any :7000];                    // Setup the address for the server

  printf("Open the server..\n");

  if ([srvr open :[addr family] :[DSocket protocol :"udp"]])
  {
    printf("Start the server (and for a client connection) ..\n");

    if (![srvr start :addr])
      printf("Could not start the server ..\n");
  }
  else
    printf("Could not open the server ..\n");

  [srvr free];                         // Cleanup
  [addr free];

  return 0;
}

